<?php
    class AdminDthink_CongratulationsController extends Main_Controller_Action
    {
        private $_arrParam;
        private $_model;
        private $_fullToday;
        
        public function init() {
            $this->loadTemplates('admin');
            $this->_arrParam = $this->_request->getParams();
            $this->_model = new AdminDthink_Model_Congratulations();
            $this->_fullToday = date('Y-m-d H:i:s');
        }
        
        public function indexAction() {
            $this->view->headTitle('Quản lý lời chúc');
            
            if($_POST['add']) {
                $this->_redirect('admin-dthink/congratulations/create');
            }
            if($_POST['refresh']) {
                $this->_redirect('adimn-dthink/congratulations');
            }
            if($_POST['delete']) {
                $listId = $this->_arrParam['check'];
                $this->_model->deleteManyCongratulations($listId);
                $this->_redirect($_SERVER['HTTP_REFERER']);
            }
            
            $adapter = new Zend_Paginator_Adapter_DbSelect($this->_model->listCongratulations());
            $paginator = new Zend_Paginator($adapter);
            $paginator->setItemCountPerPage(40);
            $paginator->setPageRange(5);
            $current = $this->_request->getParam('page', 1);
            $paginator->setCurrentPageNumber($current);
            
            $this->view->data = $paginator;
            $this->view->total = $paginator->getTotalItemCount();
        }
        
        public function createAction() {
            $this->view->headTitle('Tạo lời chúc mới');
            
            if($this->_request->isPost()) {
                $trim = new Zend_Filter_StringTrim();
                $fullname = $trim->filter($this->_arrParam['fullname']);
                $content = $trim->filter(strip_tags($this->_arrParam['content_text']));
                $flag = true;
                
                $validate = new AdminDthink_Form_ValidateCongratulations();
                if($validate->validFullname($fullname)) {
                    $this->view->fullnameValue = $fullname;
                }
                else {
                    $this->view->errorFullname = 'Tên đầy đủ chứa chữ cái, chữ số và độ dài 1-50 kí tự';
                    $flag = false;
                }
                if($validate->validContent($content)) {
                    $this->view->contentValue = $content;
                }
                else {
                    $this->view->errorContent = 'Nội dung không được để trống';
                    $flag = false;
                }
                
                if($flag == true) {
                    $flex = new Zendvn_Filter_RemoveCircumflex();
                    $lower = new Zend_Filter_StringToLower();
                    $data = array(
                        'fullname'=>$fullname,
                        'content'=>$content,
                        'create_time'=>$this->_fullToday,
                        'status'=>0,
                        'fullname_plaintext'=>$lower->filter($flex->filter($fullname)),
                        'content_plaintext'=>$lower->filter($flex->filter($content)),
                    );
                    $this->_model->createCongratulations($data);
                    $this->_redirect('admin-dthink/congratulations');
                }
            }
        }
        
        public function editAction() {
            $this->view->headTitle('Sửa lời chúc');
            $id = (int) $this->_arrParam['id'];
            $this->view->data = $this->_model->getCongratulationsById($id);
            
            if($this->_request->isPost()) {
                $trim = new Zend_Filter_StringTrim();
                $fullname = $trim->filter($this->_arrParam['fullname']);
                $content = $trim->filter(strip_tags($this->_arrParam['content_text']));
                $status = (int) $this->_arrParam['status'];
                $flag = true;
                
                $validate = new AdminDthink_Form_ValidateCongratulations();
                if(!$validate->validFullname($fullname)) {
                    $this->view->errorFullname = 'Tên đầy đủ chứa chữ cái, chữ số và độ dài 1-50 kí tự';
                    $flag = false;
                }
                if(!$validate->validContent($content)) {
                    $this->view->errorContent = 'Nội dung không được để trống';
                    $flag = false;
                }
                
                if($flag == true) {
                    $flex = new Zendvn_Filter_RemoveCircumflex();
                    $lower = new Zend_Filter_StringToLower();
                    $data = array(
                        'fullname'=>$fullname,
                        'content'=>$content,
                        'status'=>$status,
                        'fullname_plaintext'=>$lower->filter($flex->filter($fullname)),
                        'content_plaintext'=>$lower->filter($flex->filter($content)),
                    );
                    $this->_model->editCongratulations($data, $id);
                    $this->_redirect('admin-dthink/congratulations');
                }
            }
        }
        
        public function deleteAction() {
            $id = (int) $this->_arrParam['id'];
            $this->_model->deleteCongratulations($id);
            $this->_redirect($_SERVER['HTTP_REFERER']);
        }
        
        public function publishAction() {
            $id = (int) $this->_arrParam['id'];
            $status = (int) $this->_arrParam['status'];
            $status = ($status == 1) ? 0 : 1;
            $data = array(
                'status' => $status,
            );
            $this->_model->publishCongratulations($data, $id);
            $this->_redirect($_SERVER['HTTP_REFERER']);
        }
        
        public function searchAction() {
            $this->view->headTitle('Tìm kiếm lời chúc');
            $filter = new Zend_Filter_StringTrim();
            $flex = new Zendvn_Filter_RemoveCircumflex();
            $toLower = new Zend_Filter_StringToLower();
            $keyword = (string) $filter->filter($this->_arrParam['keyword']);
            $this->view->searchvalue = $keyword;
            $keyword = $toLower->filter($flex->filter($keyword));
            $flag = true;

            if($_POST['add']) {
                $this->_redirect('admin-dthink/congratulations/create');
            }
            if($_POST['refresh']) {
                $this->_redirect('admin-dthink/congratulations');
            }
            if($_POST['delete']) {
                $listId = $this->_arrParam['check'];
                $result = $this->_model->deleteManyCongratulations($listId);
                $this->_redirect($_SERVER['HTTP_REFERER']);
            }

            $validation = new AdminDthink_Form_ValidateCongratulations();
            if($validation->validSearch($keyword)) {
                $this->view->searchValue = $keyword;
            }
            else {
                $this->view->errorSearch = 'Từ tìm kiếm có độ dài từ 3-50 kí tự';
                $flag = false;
            }

            if($flag == true) {
                $adapter = new Zend_Paginator_Adapter_DbSelect($this->_model->searchCongratulations($keyword));
                $paginator = new Zend_Paginator($adapter);
                $paginator->setItemCountPerPage(40);
                $paginator->setPageRange(5);
                $currentPage = $this->_request->getParam('page', 1);
                $paginator->setCurrentPageNumber($currentPage);

                $this->view->data = $paginator;
                $this->view->total = $paginator->getTotalItemCount();
            }
        }
    }
